import requests
from biz.utils.logger import logger
from requests.exceptions import RequestException

class RequestUtil:
    @staticmethod
    def get(url, params=None, headers=None, timeout=30):
        logger.info(f"正在发起GET请求到 {url}，参数: {params}, 头部: {headers}")
        """
        发送GET请求
        :param url: 请求的URL
        :param params: 请求参数
        :param headers: 请求头
        :param timeout: 超时时间
        :return: 请求的响应
        """
        try:
            response = requests.get(url, params=params, headers=headers, timeout=timeout)
            logger.info(f"请求结果: {response.text}")
            response.raise_for_status()
            return response
        except RequestException as e:
            logger.error(f"请求失败: {e}")
            return None

    @staticmethod
    def post(url, data=None, json=None, headers=None, timeout=30):
        logger.info(f"正在发起POST请求到 {url}，数据: {data}, JSON: {json}, 头部: {headers}")
        """
        发送POST请求
        :param url: 请求的URL
        :param data: 表单数据
        :param json: JSON数据
        :param headers: 请求头
        :param timeout: 超时时间
        :return: 请求的响应
        """
        try:
            response = requests.post(url, data=data, json=json, headers=headers, timeout=timeout)
            logger.info(f"请求结果: {response.text}")
            response.raise_for_status()
            return response
        except RequestException as e:
            logger.error(f"请求失败: {e}")
            return None
    @staticmethod
    def post_form(url, data=None, files=None, headers=None, timeout=60):
        logger.info(f"正在发起POST表单请求到 {url}，数据: {data}, 文件: {files}, 头部: {headers}")
        try:
            response = requests.post(url, data=data, files=files, headers=headers, timeout=timeout)
            logger.info(f"请求结果: {response.text}")
            response.raise_for_status()
            return response
        except RequestException as e:
            logger.error(f"请求失败: {e}")
            return None

    @staticmethod
    def post_form_urlencoded(url, data=None, headers=None, timeout=60):
        """
        发送application/x-www-form-urlencoded格式的POST请求
        :param url: 请求的URL
        :param data: 表单数据
        :param headers: 请求头
        :param timeout: 超时时间
        :return: 请求的响应
        """
        if headers is None:
            headers = {}
        headers['Content-Type'] = 'application/x-www-form-urlencoded'
        logger.info(f"正在发起x-www-form-urlencoded请求到 {url}，数据: {data}, 头部: {headers}")
        try:
            response = requests.post(url, data=data, headers=headers, timeout=timeout)
            logger.info(f"请求结果: {response.text}")
            response.raise_for_status()
            return response
        except RequestException as e:
            logger.error(f"请求失败: {e}")
            return None
# 使用示例
if __name__ == "__main__":
    util = RequestUtil()
    # response_get = util.get("http://httpbin.org/get", params={"key": "value"})
    # if response_get:
    #     print(response_get.text)  # 打印响应内容
    #
    # response_post = util.post("http://httpbin.org/post", json={"key": "value"})
    # if response_post:
    #     print(response_post.text)  # 打印响应内容


    # POST form请求示例
    response_post_form = util.post_form("http://123.232.113.233:55000/jwtcenter/JWTInfoController/getToken", data={"subject": "telchina", "loginPwd": "545DBA10DECB60A1F33E7D8BCEE34FE3B7E1621208DC14F741C910DDF4FC0FB9EEE9592E7C4E0D41191833549F8C7CA9"})
    if response_post_form:
        print(response_post_form.text)